package sg.simple.snmp.provider.impl;

import isnmp.ui.dto.CPUInfo;
import isnmp.ui.dto.CPUInfo.CPU_PROPERTY;

import java.util.ArrayList;
import java.util.List;

import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.util.TableEvent;

public class CPUInfoProvider extends SNMPTableReader {

	/** Conceptual entry to mark the index in table */
	private String processorTableIndex = "1.3.6.1.2.1.25.3.3.1";

	/** contains the firmware ID */
	private String processorfirmWareOid = "1.3.6.1.2.1.25.3.3.1.1";

	/** Average load percentage over the last minite */
	private String processorLoadPercentageOid = "1.3.6.1.2.1.25.3.3.1.2";

	//private String processorDeviceDescOid = "1.3.6.1.2.1.25.3.2.1.2";

	public List<CPUInfo> retrieve(Snmp session, Target target) {

		OID firmWareID = new OID(processorfirmWareOid);
		OID loadPercentage = new OID(processorLoadPercentageOid);
		//OID processorDeviceDesc = new OID(processorDeviceDescOid);

		List<CPUInfo> cpuInformationList = new ArrayList<CPUInfo>();

		List<TableEvent> cpuInfoTable = this.readTable(session, target,
				new OID[] {/* processorDeviceDesc,*/ firmWareID, loadPercentage });

		for (TableEvent aRow : cpuInfoTable) {

			if (aRow.isError()) {
				continue;
			}

			String firmware = "";
			String load = "";
			String desc = "";
			for (VariableBinding v : aRow.getColumns()) {
				if (matches(firmWareID, v)) {
					firmware = v.getVariable().toString();
					continue;
				}
				if (matches(loadPercentage, v)) {
					load = v.getVariable().toString();
					continue;
				}
				/*
				if (matches(processorDeviceDesc, v)) {
					desc = v.getVariable().toString();
					continue;
				}
				*/
			}

			CPUInfo info = new CPUInfo();
			info.putValue(CPU_PROPERTY.LOAD, load);
			info.putValue(CPU_PROPERTY.FIRM_WARE_ID, firmware);
			//info.putValue(CPU_PROPERTY.DEVICE_TYPE, desc);
			cpuInformationList.add(info);
		}

		return cpuInformationList;

	}
}
